<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<style>
    #editReportPlanForm label {
        padding-top: 0px;
    }

</style>
<body class="hold-transition skin-blue sidebar-mini">
<!--分配-->
<div class="modal fade" id="modal-edit">
    <div class="modal-dialog" style="width:1000px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">分配计划员</h4>
            </div>
            <div class="modal-body" style="height: 750px">
                <form id="editReportPlanForm" class="form-horizontal">
                    <input type="hidden" name="rppId">
                    <div class="col-md-12">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label" style="padding: 6px 15px;">计划员</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="rppPlanner" autocomplete="off"
                                           placeholder="请输入计划员">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label" style="padding: 6px 15px;">计划号</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="rppPlanNo" autocomplete="off"
                                           placeholder="请输入计划号">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">项目编号</label>
                                <div class="col-sm-10">
                                    <span id="rppProjectCode"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">项目名称</label>
                                <div class="col-sm-10">
                                    <span id="rppProjectName"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">物料描述</label>
                                <div class="col-sm-10">
                                    <span id="rppMatDesc"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">数量</label>
                                <div class="col-sm-8">
                                    <span id="rppNum"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">单位</label>
                                <div class="col-sm-8">
                                    <span id="rppUnit"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">采购类型</label>
                                <div class="col-sm-8">
                                    <span id="rppTypes"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6" id="rppUseStockByModal">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">物料类型</label>
                                <div class="col-sm-8">
                                    <span id="rppMalType"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">部分</label>
                                <div class="col-sm-8">
                                    <span id="rppPart"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">型号/标准号</label>
                                <div class="col-sm-8">
                                    <span id="rppStandardNo"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">品牌</label>
                                <div class="col-sm-8">
                                    <span id="rppBrand"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">备注</label>
                                <div class="col-sm-10">
                                    <span id="rppRemark"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">使用库存</label>
                                <div class="col-sm-10">
                                    <span id="rppUseStore"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">图号</label>
                                <div class="col-sm-10">
                                    <span id="rppDiagram"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">外协类型</label>
                                <div class="col-sm-10">
                                    <span id="rppAssistType"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">提报人</label>
                                <div class="col-sm-10">
                                    <span id="rppDesigner"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">提报人联系方式</label>
                                <div class="col-sm-10">
                                    <span id="rppDesignerTel"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">提报人所属部门</label>
                                <div class="col-sm-10">
                                    <span id="rppDesignerDept"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">接收时间</label>
                                <div class="col-sm-10">
                                    <span id="rppRecTime"></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" shiro:hasPermission="buy:plandistribution:edit" class="btn btn-info" id="btn-edit-report-plan">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<!--导入-->
<div class="modal fade" id="modal-import">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Excel信息导入</h4>
            </div>
            <div class="modal-body">
                <form id="importForm">
                    <div class="form-group">
                        <label>上次导入错误数据</label>
                        <a id="download-error"
                           href="/buy/getReportPlanList/edit/exportErrorLog">点击下载</a>
                    </div>
                    <!--<div class="form-group">
                        <label for="yearMonth">生产日报上传年月份</label>
                        <input type="text" class="form-control col-md-3" id="yearMonth" name="yearMonth"
                               th:value="${#dates.format(date, 'yyyy-MM')}" readonly="readonly"/>
                    </div>-->
                    <div class="form-group">
                        <label>选择Excel文件并上传</label>
                        <input class="btn" type="file" multiple="multiple" name="file">
                        <p class="help-block">请按模板格式进行导入，信息与模板信息对应。</p>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button shiro:hasPermission="buy:plandistribution:add" id="btn-upload" type="button" class="btn btn-success">
                    <span class="Bold">上传</span>
                </button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<section class="content-header"><h1>提报计划分配</h1></section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>项目编号</label>
                                    <input type="text" name="rppProjectCode" class="form-control" placeholder="请输入项目编号">
                                </div>
                                <div class="form-group">
                                    <label>项目名称</label>
                                    <input type="text" name="rppProjectName" class="form-control" placeholder="请输入项目名称">
                                </div>
                                <div class="form-group">
                                    <label>物料描述</label>
                                    <input type="text" name="rppMatDesc" class="form-control" placeholder="请输入物料描述">
                                </div>
                                <div class="form-group">
                                    <label>提报人</label>
                                    <input type="text" name="rppDesigner" class="form-control" placeholder="请输入提报人">
                                </div>
                                <div class="form-group">
                                    <label>标准号</label>
                                    <input type="text" name="rppStandardNo" class="form-control" placeholder="请输入标准号">
                                </div>
                                <div class="form-group">
                                    <label>品牌</label>
                                    <input type="text" name="rppBrand" class="form-control" placeholder="请输入品牌">
                                </div>
                                <div class="form-group">
                                    <label>计划员</label>
                                    <input type="text" name="rppPlanner" class="form-control" placeholder="请输入计划员">
                                </div>
                                <div class="form-group">
                                    <label>编辑状态</label>
                                    <select name="rppEditFlag" class="form-control">
                                        <option value="2">已编辑</option>
                                        <option value="1">未编辑</option>
                                        <option value="" selected="selected">全部</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>提交状态</label>
                                    <select name="rppPostPlannerStatus" class="form-control">
                                        <option value="2">已提交</option>
                                        <option value="1" selected="selected">未提交</option>
                                        <option value="">全部</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>采购类型</label>
                                    <select name="rppTypeId" class="form-control">
                                        <option value="1">原材料</option>
                                        <option value="2">外购件</option>
                                        <option value="3">外协件</option>
                                        <option value="" selected="">全部</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>物料类型</label>
                                    <select name="rppMalType" class="form-control">
                                        <option value="" selected="selected">全部</option>
                                        <option value="机械">机械</option>
                                        <option value="流体">流体</option>
                                        <option value="自动化">自动化</option>
                                        <option value="外观（包括油漆）">外观（包括油漆）</option>
                                        <option value="消耗品（包括气体）">消耗品（包括气体）</option>
                                        <option value="维修备件">维修备件</option>
                                        <option value="办公用品">办公用品</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>接收时间</label>
                                    <input type="text" class="form-control datepicker" name="startTime"
                                           readonly="readonly"/>
                                    <span> 至 </span>
                                    <input type="text" class="form-control datepicker" name="endTime"
                                           readonly="readonly"/>
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search">
                                    <span class="Bold">搜索</span>
                                </button>
                            </div>

                        </div>
                    </form>
                    <form id="exportExcel" action="/buy/getReportPlanList/edit/export" style="display: none">
                        <input name="rppEditFlag" value="1" />
                    </form>
                    <div class="row" id="toolbar">
                        <div class="form-inline">
                            <button shiro:hasPermission="buy:plandistribution:edit" class="btn btn-success pull-right" style="margin-right: 10px"
                                    onclick="submitAll()">
                                <span class="Bold">批量提交</span>
                            </button>
                            <button shiro:hasPermission="buy:plandistribution:export" id="btn_export" class="btn btn-primary pull-right" style="margin-right: 10px">
                                <span class="Bold">导出</span>
                            </button>
                            <button shiro:hasPermission="buy:plandistribution:add" id="btn_import" class="btn btn-info pull-right" style="margin-right: 10px">
                                <span class="Bold">导入</span>
                            </button>
                        </div>
                    </div>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    let statusText = ["", "未编辑", "已编辑"];
    let submitStatusText = ["", "未提交", "已提交"];
    let statusColor = ["", "label-warning", "label-success"];


    /** 表格 */
    let table;
    $(function () {
        $("title").html($("title").html()+" —— 提报计划分配");
        /** 初始化表格 */
        table = js.table.init({
            url: ctx + "buy/getReportPlanList/edit/list",
            showColumns: true,
            pageSize: 300,
            uniqueId: 'rppId',
            paginationVAlign: 'both',
            showExport: false,
            columns: [
                {
                    checkbox: true,
                    width: '30px',
                    formatter: function (value, row, index) {
                        // 正常提报并且数据正常状态可以提交选择
                        if (row.rppEditFlag === 2) {
                            return {disabled: false}
                        } else {
                            return {disabled: true}
                        }
                    }
                },
                {
                    title: '序号', field: 'rppId', width: '50px',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: "计划员", field: 'rppPlanner'},
                {title: "计划号", field: 'rppPlanNo'},
                {title: "项目编号", field: 'rppProjectCode'},
                {title: "项目名称", field: 'rppProjectName'},
                {title: "物料描述", field: 'rppMatDesc'},
                {title: "数量", field: 'rppNum'},
                {title: "单位", field: 'rppUnit'},
                {
                    title: "采购类型", field: 'rppTypes'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        }  else {
                            return "--";
                        }
                    }
                },
                {title: "物料类型", field: 'rppMalType',visible: false},
                {
                    title: "部分", field: 'rppPart',visible: false
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        }  else {
                            return "--";
                        }
                    }
                },
                {
                    title: "标准号", field: 'rppStandardNo'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        }  else {
                            return "--";
                        }
                    }
                },
                {
                    title: "品牌", field: 'rppBrand'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        }  else {
                            return "--";
                        }
                    }
                },
                {
                    title: "备注", field: 'rppRemark'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        }  else {
                            return "--";
                        }
                    }
                },
                {
                    title: "是否使<br>用库存", field: 'rppUseStore',
                    formatter: function (value, row, index) {
                        if (value === null) {
                            return "--";
                        }else if (value === 0) {
                            return "否";
                        } else {
                            return "是";
                        }
                    }
                },
                {
                    title: "图号", field: 'rppDiagram',visible: false
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        }  else {
                            return "--";
                        }
                    }
                },
                {
                    title: "外协类型", field: 'rppAssistType',visible: false
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        }  else {
                            return "--";
                        }
                    }
                },
                {title: "提报人", field: 'rppDesigner'},
                {title: "联系方式", field: 'rppDesignerTel',visible: false},
                {title: "提报人<br>部门", field: 'rppBranchName'},
                {title: "接收时间", field: 'rppRecTime', width: '90px'},
                {
                    title: "编辑状态", field: 'rppEditFlag',
                    formatter: function (value, row, index) {
                        return '<span class="badge ' + statusColor[value] + '">' + statusText[value] + '</span>';
                    }
                },
                {
                    title: "提交状态", field: 'rppPostPlannerStatus',
                    formatter: function (value, row, index) {
                        return '<span class="badge ' + statusColor[value] + '">' + submitStatusText[value] + '</span>';
                    }
                },
                {
                    title: "操作",
                    width: '95px',
                    formatter: function (value, row, index) {
                        let actions = [];
                        if(hasP('buy:plandistribution:edit')){
                            //未提交
                            if (row.rppPostPlannerStatus === 1) {
                                actions.push('<button class="btn btn-warning btn-xs" onclick="showEditPanel(' + row.rppId + ')">编辑</button> ');
                            }
                            //已提交 未改变提报状态 未拆分过计划
                            else if (row.rppPostPlannerStatus === 2 &&(row.rppUseStock == null || row.rppUseStock == 0) && row.rppSplitPlanType === 'source'){
                                actions.push('<button class="btn btn-warning btn-xs" onclick="showEditPanel(' + row.rppId + ')">编辑</button> ');
                            }
                            //已提交 已改变提报状态 未拆分过计划
                            else if(row.rppPostPlannerStatus === 2 && (row.rppUseStock != null && row.rppUseStock != 0) && row.rppSplitPlanType === 'source'){
                                actions.push('<p style="word-wrap: break-word;width: 85px">已改变提报状态，无法重新分配。</p>');
                            }
                            //已提交 未改变提报状态 已拆分过计划
                            else if(row.rppPostPlannerStatus === 2 && row.rppUseStock == null && row.rppSplitPlanType != 'source'){
                                actions.push('<p style="word-wrap: break-word;width: 85px">已拆分计划，无法重新分配。</p>');
                            }
                            //已提交 已改变提报状态 已拆分过计划
                            else if(row.rppPostPlannerStatus === 2 && row.rppUseStock != null && row.rppSplitPlanType != 'source'){
                                actions.push('<p style="word-wrap: break-word;width: 85px">已改变提报状态并已拆分计划，无法重新分配。</p>');
                            }
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /** 添加表单验证规则 */
        js.validate.init("editReportPlanForm", {
            fields: {
                rppPlanner: {
                    validators: {
                        notEmpty: {message: '请输入计划员'},
                        threshold: 1, //有1字符以上才发送ajax请求，（input中输入一个字符，插件会向服务器发送一次，设置限制，6字符以上才开始）
                        callback: {
                            message: "计划员不存在",
                            callback: function (value, validator) {
                                var flag = false;
                                if (value.length >= 1) {
                                    $.ajax({
                                        url: ctx + 'buy/getReportPlanList/edit/isExits', // 请求路径
                                        type: 'POST',
                                        async: false,	// 同步请求，必须是false
                                        data: {
                                            rppPlanner: value // 请求参数
                                        },
                                        success: function (res) {
                                            flag = $.parseJSON(res).valid;
                                            if (flag == false) {
                                                $("#btn_save").show();
                                            }
                                        }
                                    });
                                    return flag;
                                }

                            }
                        }
                    },
                },
            }

        });


        /** 表格搜索 */
        $("#btn-search").click(function () {
            js.table.search(table);
        });
        /** 分配计划员 */
        $("#btn-edit-report-plan").click(function () {
            js.validSubmit({
                formId: "editReportPlanForm",
                url: ctx + "buy/getReportPlanList/edit/save",
                data: new FormData($("#editReportPlanForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.modal.hide("modal-edit");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        });
        /** 打开导入面板 */
        $("#btn-import").click(function () {
            js.modal.open("modal-import");
        });
        /** 导出 */
        $("#btn_export").click(function () {
            $("#exportExcel").submit();
        });
        /** 导入 */
        $("#btn_import").click(function () {
            js.modal.open('modal-import');
        });

        /** 导入提报计划 */
        $("#btn-upload").click(function () {
            var $this = $(this);
            $this.attr('disabled', true);
            js.validSubmit({
                formId: "importForm",
                url: ctx + "buy/getReportPlanList/edit/import",
                data: new FormData($("#importForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.alert(result.msg);
                        if (result.msg == '导入完成') {
                            js.modal.hide("modal-import");
                        }
                        js.table.refresh(table);
                        // js.modal.hide("modal-import");
                        $this.attr('disabled', false);
                    }
                }
            });
        });
    });

    /**
     * 打开分配框
     */
    function showEditPanel(id) {
        let row = $("#bootstrap-table").bootstrapTable('getRowByUniqueId', id);//根据toComID读取选中行在表tableParam中
        $("#editReportPlanForm").fillData(row);
        $("#editReportPlanForm").fillSpan(row);
        if ($("#rppUseStore").html() === '1') {
            $("#rppUseStore").html("是");
        } else {
            $("#rppUseStore").html("否");
        }
        js.modal.open("modal-edit");
    }

    /**
     * 批量提交
     */
    function submitAll() {
        let arr = js.table.selectColumns(table, "rppId");
        if (arr.length < 1) {
            js.modal.alert("请选择要提交的计划");
            return;
        } else {
            let ids = "";
            for (let i = 0; i < arr.length; i++) {
                ids += arr[i] + ",";
            }
            ids = ids.substring(0, ids.length - 1);
            js.modal.confirm("您确认要提交这些计划吗？", function () {
                submit(ids);
            })

        }
    }
    /**
     * 提交
     */
    function submit(ids) {
        js.post({
            url: ctx + 'buy/getReportPlanList/edit/submit',
            type: 'post',
            traditional: true,            // 阻止深度序列化， 使参数可以使用数组
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            data: {
                rppIds: ids
            },
            async: true,                // 异步执行
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                } else {
                    js.modal.warning(result.msg);
                }
            },
            error: function () {
                js.modal.fail();
            }

        });
    }
</script>
</body>
</html>
